class DashboardController < ApplicationController
    def index
        @total_score = 0
        if authorize == true
          @assignments = Assignemnt.find_all_assignemnts()
          @students = Person.find_by_sql("select p.* from people p, users u where u.role = 2 AND u.username = p.user order by p.surname")
          @stuInfo = {}
          @grades = {}
          @students.each{ |stu|
            @grades[stu.id] = []
            @assignments.each{ |ass|
                @grades[stu.id] << GradeEntry.get_student_grades(stu.id, ass.id)
            }
            @stuInfo["#{stu.user}"] = 'username: ' + stu.user.to_s
          }
          if session[:loginrole] == "Admin"
              @dashboard = "Go to Gradebook"
              @studentss = getStudents()
              @assignmentss = getAssignments()
          end
        end
    end
    
    
    def save_grades
        numAssigns = params[:assignmentsPerRow].to_i
        @assignments = Assignemnt.find_all_assignemnts()
        @students = Person.find_by_sql("select p.* from people p, users u where u.role = 2 AND u.username = p.user order by p.surname")

        i = 0
        @students.each{ |stu|
            @assignments.each{ |ass|
                entry = GradeEntry.get_student_grades(stu.id, ass.id)
                if entry.nil?
                    GradeEntry.create(:person_id => stu.id, :assignment_id => ass.id, :grade => params["#{i}"])
                else
                    entry.update_attribute(:grade, params["#{i}"])
                end
                #puts stu.id.to_s + "  " + ass.id.to_s + "  " + params["#{i}"].to_s
                i = i + 1
            }
        }
        redirect_to :controller => :dashboard, :action => :index
    end
  
    def addAssignment
        if params[:newAssignment] != "<assignment name>" and params[:newPoints].to_f >= 0.0 and params[:newPoints] != "<points possible>"
            if params[:newAssignmentDesc] == "<assignment description>"
                params[:newAssignmentDesc] = ""
            end
    		ass = Assignemnt.create(:title => params[:newAssignment], :points_possible => params[:newPoints], :description => params[:newAssignmentDesc])
		else
		    flash[:notice] = "Invalid assignment entry."
		end
        redirect_to :controller => :dashboard, :action => :index
    end
  
    def removeAllAssignments
        flash[:notice] = ""
        @assignments = Assignemnt.find_all_assignemnts()
        if not @assignments.empty?
            @assignments.each{ |ass|
                ass.delete
            }
        else
    		flash[:notice] += "No assignments to delete.<br />"
        end
        @grades = GradeEntry.find(:all)
        if not @grades.empty?
            @grades.each{ |grade|
                grade.delete
            }
        else
    		flash[:notice] += "No grades to delete.<br />"
        end
        redirect_to :controller => :dashboard, :action => :index
    end
  
    def removeAssignment
        ass = Assignemnt.find(:first, :conditions => [ "title = ?", params[:oldAssignment]])
        if ass.nil?
    		flash[:notice] = "That assignment does not exist."
            redirect_to :controller => :dashboard, :action => :index
            return
        end
        
        @grades = GradeEntry.find(:all, :conditions => [ "assignment_id = ?", ass.id])
        if not @grades.nil?
            @grades.each{|grade|
                grade.delete
            }
        end
        
        ass.delete
        
        redirect_to :controller => :dashboard, :action => :index
    end
  
    def removeAllStudents  
        @grades = GradeEntry.find(:all)
        @grades.each{ |i|
            i.delete
        }
        @person = Person.find(:all)
        @person.each{ |i|
            i.delete
        }
        @contact = Contact.find(:all)
        @contact.each{ |i|
            i.delete
        }
        @user = User.find(:all)
        @user.each{ |i|
            i.delete
        }
        @rates = RatingSubmitted.find(:all)
        @rates.each{ |i|
            i.delete
        }
        redirect_to :controller => :dashboard, :action => :index
    end

    def removeStudent  
        fName, lName = params[:username].chomp.split
        stu = Person.find(:first, :conditions => [ "firstname = ? and surname = ?", fName, lName])
        #puts "USER: " + stu.user.to_s
        #return
        #stu = Person.find(:first, :conditions => [ "user = ?", params[:username]])
        if stu == nil
			flash[:notice] = "That user does not exist."
            redirect_to :controller => :dashboard, :action => :index
            return
        end
        @grades = GradeEntry.find(:all, :conditions => [ "person_id = ?", stu.id])
        if @grades != nil
            @grades.each{ |grade|
                grade.delete
            }
        end
        
        @rates = RatingSubmitted.find(:first, :conditions => [ "person_id = ?", stu.id])
        @rates.delete unless @rates.nil?
        
        @contact = Contact.find(:first, :conditions => [ "id = ?", stu.id])
        @contact.delete unless @contact.nil?
        
        @user = User.find(:first, :conditions => [ "username = ?", stu.user])
        @user.delete unless @user.nil?
        
        @person = stu
        @person.delete
        
        redirect_to :controller => :dashboard, :action => :index
    end
    
    def getStudents()
        value = "<option selected='selected'>-Select Student-</option>"
		#majors = Major.find(:all, :order => "name")
		@students.each{ |stu|
            value += "<option>#{stu[:firstname]} #{stu[:surname]}</option>"
		}
		value
	end
  
    def getAssignments()
        value = "<option selected='selected'>-Select Assignment-</option>"
		#majors = Major.find(:all, :order => "name")
		@assignments.each{ |ass|
            value += "<option>#{ass[:title]} </option>"
		}
		value
	end
  
    def authorize 
        unless session[:loggedin] == true && session[:loginrole] == "Admin"
            flash[:notice] = "Please log in" 
            redirect_to :controller => :login, :action => :index
            return false
        end
        return true 
    end 
  
  

end
